﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using Transcend.FileUploader.Dao;
using Transcend.FileUploader.Entity;
using System.Security;
using Transcend.FileUploader.Utility;
using System.Threading;
using Microsoft.Win32;
using System.Data.Common;

namespace Transcend.FileUploader
{

    public partial class UserConfigWindow : Form
    {
        private readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public UploadSetting UploadSetting { get; set; }

        public UserConfigWindow(UploadSetting uploadSetting)
        {
            InitializeComponent();
            UploadSetting = uploadSetting;
            ConfigUI(UploadSetting);
        }

        private void UploadSettingWindow_Load(object sender, EventArgs e)
        {
            try
            {
                ReadUserConfig();
            }
            catch (Exception ex)
            {
                logger.Error("读取个人上传配置出错", ex);
                MessageBox.Show("读取个人上传配置出错", "错误");
                return;
            }
        }

        private void Bn_SaveSetting_Click(object sender, EventArgs e)
        {
            try
            {

                // 设置校验
                if (!Cb_DocNo.Checked && !Cb_DocId.Checked)
                {
                    MessageBox.Show("档案号/宗号其中之一为必选项", "保存失败");
                    return;
                }
                if (chkEncrypt.Checked)
                {
                    if (string.IsNullOrEmpty(txtPassword.Text.Trim()))
                    {
                        MessageBox.Show("图形文件为加密状态时，导入系统需要提供密码。");
                        return;
                    }
                }

                UploadSetting uploadSetting = new UploadSetting();
                uploadSetting.ParseVolNo = Cb_VolNo.Checked;
                uploadSetting.ParseDocNo = Cb_DocNo.Checked;
                uploadSetting.ParseDocId = Cb_DocId.Checked;
                uploadSetting.IgnoreError = Cb_IgnoreError.Checked;
                uploadSetting.GenDsItemUseCatalog = Cb_GenDsItemUseCatalog.Checked;
                uploadSetting.OverrideWarm = Cb_OverrideWarm.Checked;
                uploadSetting.EncryptedImage = chkEncrypt.Checked;   //记录文件是否已经加密
                uploadSetting.ImagePassword = txtPassword.Text.Trim();

                
                uploadSetting.isVolume = radioButton_volume.Checked;
                uploadSetting.isCase = radioButton_case.Checked;
                uploadSetting.isAutoAdd = checkBox_isAutoAdd.Checked;

                SaveUploadSetting(uploadSetting);
                UploadSetting = uploadSetting;
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                logger.Error("保存配置出错", ex);
                MessageBox.Show(string.Format("保存配置出错:{0}", ex.Message));
            }
        }

        private void Bn_Close_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }


        /// <summary>
        /// 从注册表读取上传配置信息
        /// </summary>
        /// <returns></returns>
        public static UploadSetting ReadUserConfig()
        {

            UploadSetting setting = null;

            RegistryKey sub = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Transcend\FileUploader");
            object itemValue = null;
            if (sub != null)
            {
                setting = new UploadSetting();
                setting.AppType = new DocStorageAppType();

                itemValue = sub.GetValue("PARSEVOLNO");
                setting.ParseVolNo = itemValue == null ? false : Boolean.Parse(itemValue.ToString());
                itemValue = sub.GetValue("PARSEDOCNO");
                setting.ParseDocNo = itemValue == null ? false : Boolean.Parse(itemValue.ToString());
                itemValue = sub.GetValue("PARSEDOCID");
                setting.ParseDocId = itemValue == null ? false : Boolean.Parse(itemValue.ToString());
                itemValue = sub.GetValue("OVERRIDEWARM");
                setting.OverrideWarm = itemValue == null ? true : Boolean.Parse(itemValue.ToString());
                itemValue = sub.GetValue("IGNOREERROR");
                setting.IgnoreError = itemValue == null ? true : Boolean.Parse(itemValue.ToString());
                itemValue = sub.GetValue("GENDSITEMUSECATALOG");
                setting.GenDsItemUseCatalog = itemValue == null ? false : Boolean.Parse(itemValue.ToString());

                itemValue = sub.GetValue("ENCRYPTEDIMAGE");   //是否加密文件
                setting.EncryptedImage = itemValue == null ? false : Boolean.Parse(itemValue.ToString());

                itemValue = sub.GetValue("IMAGEPASSWORD");   //是否加密文件
                setting.ImagePassword = itemValue == null ? "" : itemValue.ToString();


                itemValue = sub.GetValue("isVolume");
                setting.isVolume = itemValue == null ? false : Boolean.Parse(itemValue.ToString());
                itemValue = sub.GetValue("isCase");
                setting.isCase = itemValue == null ? false : Boolean.Parse(itemValue.ToString());
                itemValue = sub.GetValue("isAutoAdd");
                setting.isAutoAdd = itemValue == null ? false : Boolean.Parse(itemValue.ToString());
            }

            return setting;
        }

        /// <summary>
        /// 将上传设置保存到注册表
        /// </summary>
        /// <param name="setting"></param>
        public static void SaveUploadSetting(UploadSetting setting)
        {
            RegistryKey sub = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Transcend\FileUploader", true);
            if (sub == null)
            {
                sub = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Transcend\FileUploader");
            }

            sub.SetValue("PARSEVOLNO", setting.ParseVolNo.ToString());
            sub.SetValue("PARSEDOCNO", setting.ParseDocNo.ToString());
            sub.SetValue("PARSEDOCID", setting.ParseDocId.ToString());
            sub.SetValue("OVERRIDEWARM", setting.OverrideWarm.ToString());
            sub.SetValue("IGNOREERROR", setting.IgnoreError.ToString());
            sub.SetValue("GENDSITEMUSECATALOG", setting.GenDsItemUseCatalog.ToString());
            sub.SetValue("ENCRYPTEDIMAGE", setting.EncryptedImage.ToString());
            sub.SetValue("IMAGEPASSWORD", setting.ImagePassword);

            sub.SetValue("isVolume", setting.isVolume.ToString());
            sub.SetValue("isCase", setting.isCase.ToString());
            sub.SetValue("isAutoAdd", setting.isAutoAdd.ToString());
           
        }

        /// <summary>
        /// 将配置信息呈现到界面
        /// </summary>
        /// <param name="setting"></param>
        private void ConfigUI(UploadSetting setting)
        {
            if (setting == null) return;

            Cb_VolNo.Checked = setting.ParseVolNo;
            Cb_DocNo.Checked = setting.ParseDocNo;
            Cb_DocId.Checked = setting.ParseDocId;
            Cb_IgnoreError.Checked = setting.IgnoreError;
            Cb_GenDsItemUseCatalog.Checked = setting.GenDsItemUseCatalog;
            Cb_OverrideWarm.Checked = setting.OverrideWarm;
            chkEncrypt.Checked = setting.EncryptedImage;
            txtPassword.Text = setting.ImagePassword;

            checkBox_isAutoAdd.Checked = setting.isAutoAdd;
            radioButton_case.Checked = setting.isCase;
            radioButton_volume.Checked = setting.isVolume;
        }

        private void chkEncrypt_CheckedChanged(object sender, EventArgs e)
        {
            txtPassword.Enabled = chkEncrypt.Checked;
        }


    }
}